//
//  STBrandView.m
//  赛拓
//
//  Created by 王磊 on 7/28/16.
//  Copyright © 2016 立金所. All rights reserved.
//


#import "STBrandView.h"
#import "STBrandBean.h"
@interface STBrandView()<UIScrollViewDelegate>
@property (nonatomic ,strong) UIScrollView *scrollView;

@property (nonatomic ,strong) M80AttributedLabel *label;

@end
#define margin 20 * ST_RATE

@implementation STBrandView

- (void)commitInit {
    [super commitInit];
    
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    
    self.scrollView = scrollView;
    
    scrollView.showsVerticalScrollIndicator = false;
    
    scrollView.showsHorizontalScrollIndicator = false;
    
    scrollView.delegate = self;
    
    scrollView.bounces = false;
    
    [self addSubview:scrollView];
    
    M80AttributedLabel *label = [M80AttributedLabel new];
    
    self.label = label;
    
    [self addSubview:label];
    
}
- (void)updateData:(id)data {
    if (self.dataArray.count) {
        [self.dataArray removeAllObjects];
        
        [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj removeFromSuperview];
            obj = nil;
        }];
    }
    
    [self.label setAttributedText:[[NSAttributedString alloc] initWithString:@"/品牌推荐/" attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:13],NSForegroundColorAttributeName: ST_EE7D21_COLOR}]];
    
    [self.label appendAttributedText:[[NSAttributedString alloc] initWithString:@"Brand recommendation" attributes:@{NSFontAttributeName : ST_13_FONT ,NSForegroundColorAttributeName: ST_666666_COLOR}]];
    
    [self.dataArray addObjectsFromArray:data];
    
    for (int i = 0; i < self.dataArray.count; i++) {
        
        STBrandBean *brand = self.dataArray[i];
        
        UIImageView *imageView = [[UIImageView alloc]init];
        
        [imageView set_BGBannerImageViewWithString:brand.imgUrl];
        
        [self.scrollView addSubview:imageView];
        
        imageView.tag = i + 100;
        
        imageView.userInteractionEnabled = true;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapBrand:)];
        
        [imageView addGestureRecognizer:tap];
    }
    
    [self layoutScrollView];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat label_height = 40 * ST_RATE;
    
//    CGFloat height = self.k_height - label_height - margin;
    
    self.label.frame = CGRectMake(margin, 0, self.k_width - margin * 2, label_height);
    
    self.scrollView.frame = CGRectMake(0, label_height, self.k_width, 160 * ST_RATE);
    
    [self layoutScrollView];
}
- (void)layoutScrollView {
    CGFloat label_height = 40 * ST_RATE;
    
    CGFloat height = self.k_height - label_height - margin;
    
    [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        obj.frame = CGRectMake(margin + (margin + height) * idx, 0, height, height);
        
    }];
    
    self.scrollView.contentSize = CGSizeMake(self.scrollView.subviews.count * (margin + height) + margin, height);
}

- (void)tapBrand:(UITapGestureRecognizer *)gr {
    
    [self.mDelegate onItemClick:STItemClickTypeBrand withData:self.dataArray[gr.view.tag - 100] forIndexPath:nil];
}

@end
